package com.yh.patterns.Iterator.task;




import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/**
 * @Author:云尚
 * @Description:具体聚合类
 */
public class ConcreteStudent implements IStudent{
    ArrayList<Student> list=null;
    public ConcreteStudent(){
        Student[] students=new Student[3];
        list=new ArrayList<Student>();
        students[0]=new Student("stu1",10);
        students[1]=new Student("stu2",18);
        students[2]=new Student("stu3",17);
        for (int i = 0; i <3 ; i++) {
            list.add(students[i]);
        }
        //降序排列
        Collections.sort(list, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o2.getAge()-o1.getAge();
            }
        });
    }

    @Override
    public Iterator createIterator() {
        //返回具体的迭代器
        return  new StudentIterator();
    }

    //具体迭代器
    private class StudentIterator implements Iterator{
        private int index=0;
        @Override
        public void first() {
            index=0;
        }

        @Override
        public void next() {
            if(index<list.size()){
                index++;
            }
        }

        @Override
        public boolean hasNext() {
            //索引长度相等表明没有下一个元素
            return index==list.size();
        }

        @Override
        public Object currentItem() {
            return list.get(index).getAge()+","+list.get(index).getName();
        }
    }

}
